#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.common.component.handler;

import ${package}.common.bean.vo.JsonResult;
import ${package}.common.emum.LoginEnum;
import ${package}.common.emum.ReturnCodeEnum;
import ${package}.common.exception.BusinessException;
import ${package}.common.exception.NotRoleException;
import ${package}.common.exception.NotUserException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.List;
import java.util.Set;

/**
 * Description: ExceptionCenter
 *
 * @author wupanhua
 * @date 2019/8/6 15:28
 *
 * <pre>
 *              ${copyright}
 *      Copyright (c) 2019. All Rights Reserved.
 * </pre>
 */
@Slf4j
@RestControllerAdvice(basePackages = {"${package}"})
public class ExceptionCenter {

    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(ConstraintViolationException.class)
    public JsonResult<Integer> constraintViolationException(ConstraintViolationException ex){

        // 打印出错误信息
        Set<ConstraintViolation<?>> constraintViolations = ex.getConstraintViolations();
        constraintViolations.forEach(constraintViolation ->  log.error("Field: {} Illegal", constraintViolation.getPropertyPath()));

        return JsonResult.getInstant(ReturnCodeEnum.PARAMETER_NOT_MATCH, constraintViolations.size());
    }

    /**
     * Description:
     * <捕捉校验异常>
     * @author wupanhua
     * @date 15:18 2019/8/8
     * @param ex 1
     * @return JsonResult
     **/
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public JsonResult<BindingResult> validateException(MethodArgumentNotValidException ex){

        // 打印出错误信息
        BindingResult bindingResult = ex.getBindingResult();
        if (bindingResult.hasErrors()) {
            List<ObjectError> allErrors = bindingResult.getAllErrors();
            allErrors.forEach(error -> {
                FieldError fieldError = (FieldError) error;
                log.error("Valdate Error: Object: {}, Filed: {}, ErrorMessage: {}", fieldError.getObjectName(), fieldError.getField(), fieldError.getDefaultMessage());
            });
        }

        return JsonResult.getInstant(ReturnCodeEnum.PARAMETER_NOT_MATCH, bindingResult);
    }

    /**
     * Description:
     * <业务异常>
     * @author wupanhua
     * @date 15:18 2019/8/8
     * @param e 1
     * @return JsonResult
     **/
    @ResponseBody
    @ExceptionHandler(value = {BusinessException.class})
    public JsonResult<String> businessException(BusinessException e) {
        log.error("业务异常", e);
        return JsonResult.getInstant(e.getCode(), e.getMessage());
    }

    /**
     * Description:
     * <自定义异常信息拦截>
     * @author wupanhua
     * @date 15:18 2019/8/8
     * @param ex 1
     * @return JsonResult
     **/
    @ExceptionHandler(NotUserException.class)
    public JsonResult<BusinessException> myCustomizeException(NotUserException ex){
        log.error("自定义异常: ", ex);
        return JsonResult.getInstant(LoginEnum.FAIL, ex);
    }

    /**
     * Description:
     * <捕捉用户无角色异常>
     * @author wupanhua
     * @date 15:18 2019/8/8
     * @param ex 1
     * @return JsonResult
     **/
    @ExceptionHandler(NotRoleException.class)
    public JsonResult<BusinessException> notRoleException(NotRoleException ex){
        log.error("角色不存在",ex);
        return JsonResult.getInstant(LoginEnum.NOROLE, ex);
    }

    /**
     * Description:
     * <系统异常>
     * @author wupanhua
     * @date 15:18 2019/8/8
     * @param e 1
     * @return JsonResult
     **/
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(value = {RuntimeException.class})
    public JsonResult<String> systemException(RuntimeException e) {
        log.error("系统异常", e);
        return JsonResult.getInstant(ReturnCodeEnum.SYSTEM_ERROR, e.getMessage());
    }

}
